import {getBillList} from '../../api/pay.js'
import {dateFormat,shake} from '../../../utils/util.js'
import user from '../../../utils/user.js'

Page({
    data: {
        data: [],
        month: '',
        pageNum: 0,
        hasNext: true,
        searchText:'',
        totalMoney:'',
    },
    onReady(){
        user.waitLogin().then(() => {
            this.setData({
                month:dateFormat('YY-mm',new Date())
            })
            this.data.pageNum = 0
            this.data.hasNext = true
            this.scrolltolower(1)
        })
    },
    onShow(){
        
    },
    onMonthChange(event){
        this.setData({
            month: event.detail.value,
            pageNum: 1,
            hasNext: true,
            data: [],
        })
        this.getBillList()
    },
    scrolltolower(clear){
        if(this.data.hasNext){
            this.data.pageNum += 1
            this.getBillList(clear===1)
        }
    },
    getBillList(clear=0) {
        console.log(clear,'clear')
        getBillList({
            month: this.data.month,
            pageSize: 10,
            pageNum: this.data.pageNum,
            projectName:this.data.searchText
        }).then(res => {
            if(!res.hasNextPage){
                this.data.hasNext = false
            }
            let arr = []
            if(!clear)
                arr.push(...this.data.data)
            arr.push(...res.list)
            this.setData({
                month:res.month,
                totalMoney:res.totalMoney,
                data:arr
            })
        })
    },
    onGoProject(event){
        wx.navigateTo({
            url: '/packagePay/pages/bill-detail/bill-detail?id='+event.currentTarget.dataset.id,
        })
    },
    /**
     * 根据项目名称搜索
     */
    searchWorker(event){
        shake(()=>{
            const { value } = event.detail
            this.setData({
                searchText:value,
                data:[],
                pageNum:1,
                hasNext:true
            })
            this.getBillList()
        })
    }
})